package com.gs.wechatdemo.adapter.base;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.gs.wechatdemo.R;
import com.gs.wechatdemo.adapter.NineGridImageViewAdapter;
import com.gs.wechatdemo.control.NineGridImageView;
import com.gs.wechatdemo.util.DensityUtil;
import com.gs.wechatdemo.util.ImageUtil;
import com.gs.wechatdemo.util.VoiceUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class BaseViewHolder extends RecyclerView.ViewHolder {

    private SparseArray<View> mViews;
    private View convertView;

    public BaseViewHolder(View itemView) {
        super(itemView);
        convertView = itemView;
        mViews = new SparseArray<>();
    }

    public static BaseViewHolder get(Context context, ViewGroup parent, int layoutId) {
        View itemView = LayoutInflater.from(context).inflate(layoutId, parent, false);
        return new BaseViewHolder(itemView);
    }

    public <T extends View> T fd(int id) {
        View view = mViews.get(id);
        if (view == null) {
            view = convertView.findViewById(id);
            mViews.put(id, view);
        }
        return (T) view;
    }

    public void setText(int id, String text) {
        TextView tv = fd(id);
        tv.setText(text);
    }

    public void setText(int id, String text, int visible) {
        TextView tv = fd(id);
        tv.setText(text);
        tv.setVisibility(visible);
    }

    public void setVoice(int id, String url, int rule, View.OnClickListener onClickListener) {
        TextView tv = fd(id);
        int length = VoiceUtil.getVoiceLength(url);
        LinearLayout ll = fd(R.id.ll_voice);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(DensityUtil.dp2px(80 + length * 4), DensityUtil.dp2px(44));
        ll.setLayoutParams(lp);
        lp.addRule(rule, R.id.iv_head);
        tv.setText(length + " ''");
        ll.setOnClickListener(onClickListener);
    }

    public void setVisibility(int id, int visible) {
        View iv = fd(id);
        iv.setVisibility(visible);
    }

    public void setImageResource(Activity activity, int id, String url, int radius) {
        ImageView iv = fd(id);
        iv.setVisibility(View.VISIBLE);
        ImageUtil.loadRadiusImg(activity, url, iv, DensityUtil.dp2px(radius));
    }

    public void setImageResource(Activity activity, int id, String url, int radius, View.OnClickListener onClickListener) {
        ImageView iv = fd(id);
        iv.setVisibility(View.VISIBLE);
        iv.setOnClickListener(onClickListener);
        ImageUtil.loadRadiusImg(activity, url, iv, DensityUtil.dp2px(radius));
    }

    public void setImageResource(Activity activity, int id, String url, int width, int height, int radius, int rule, View.OnClickListener onClickListener) {
        ImageView iv = fd(id);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(width, height);
        lp.addRule(rule, R.id.iv_head);
        iv.setLayoutParams(lp);
        iv.setOnClickListener(onClickListener);
        ImageUtil.loadRadiusImg(activity, url, iv, DensityUtil.dp2px(radius));
    }

    public void setImageResource(Activity activity, int id, Bitmap bitmap, int width, float height, int radius, int rule, View.OnClickListener onClickListener) {
        ImageView iv = fd(id);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(width, (int) height);
        lp.addRule(rule, R.id.iv_head);
        iv.setLayoutParams(lp);
        iv.setOnClickListener(onClickListener);
        ImageUtil.loadRadiusImg(activity, bitmap, iv, DensityUtil.dp2px(radius));
    }

    private NineGridImageViewAdapter<String> mAdapter;

    public void setNineImg(Activity activity, int id, String img) {
        if (TextUtils.isEmpty(img)) {
            return;
        }
        NineGridImageView<String> nine = fd(id);
        if (mAdapter == null) {
            mAdapter = new NineGridImageViewAdapter<String>() {
                @Override
                public void onDisplayImage(Context context, ImageView imageView, String s) {
                    ImageUtil.loadImg(activity, s, imageView);
                }
            };
        }
        nine.setAdapter(mAdapter);
        String[] split = img.split(",");
        if (split.length > 1) {
            nine.setGap(DensityUtil.dp2px(2));
        }
        List<String> stringList = new ArrayList<>(Arrays.asList(split));
        nine.setImagesData(stringList);
    }
}
